<template>
  <div>
      <el-breadcrumb separator="/" style="height:10px;">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>系统管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      </el-breadcrumb>
       
      
      
      <el-table
      :data="pageinfo.list"
      border
      style="width: 100%">
      <el-table-column
        prop="cid"
        label="序号"
        width="200">
      </el-table-column>

      <el-table-column
        prop="cname"
        label="名称"
        width="200">
      </el-table-column>

      <el-table-column
        prop="cprice"
        label="价格"
        width="180">
      </el-table-column>

      <el-table-column label="图片" width="250">
        <template slot-scope="scope">
          <img :src="require('../../assets/shangpin/'+scope.row.cimg)" alt="" style="width:100%;height:100%;">
        </template>
      </el-table-column>

      <el-table-column
        prop="cnumber"
        label="剩余数量"
        width="200">
      </el-table-column>

      <el-table-column label="管理操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="checkoutFn(scope.row.cid)">修改</el-button>
        </template>
        </el-table-column>
    </el-table>
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="commodityVo.pageNum"
      :page-sizes="[5, 7, 10, 15]"
      :page-size="commodityVo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageinfo.total">
    </el-pagination>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-form ref="form" :model="commodity" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="commodity.cname"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="commodity.cprice"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="commodity.cnumber"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateCommodity">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import {findAllCommodity,checkoutBycid,updateCommodity} from '../../api'
export default {
  data() {
    return {
      pageinfo : {}  ,
      commodityVo : {
        pageNum : 1,
        pageSize : 5
      },
      commodity : {},
      dialogVisible : false
    }
  },
  created() {
    this.findAll()
  },
  methods: {
    async findAll(){
      let {data:baseRefault} = await findAllCommodity(this.commodityVo)
      this.pageinfo = baseRefault.data
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.commodityVo.pageNum = 1
      this.commodityVo.pageSize = val
      this.findAll()
    },
    handleCurrentChange(val) {
      this.commodityVo.pageNum = val
      console.log(`当前页: ${val}`);
      this.findAll()
    },
    async checkoutFn(cid){

      var staffname = sessionStorage.getItem('staffid')
      if(staffname == 'admin'){
        // 根据cid 查询出要修改的用户
        this.dialogVisible = true
        let {data:baseRefault} = await checkoutBycid(cid)
        this.commodity = baseRefault.data
      }else{
        this.$message.error('您无权修改商品操作！请联系管理员！')
      }
      
    },
    async updateCommodity(){
      let {data:baseRefault} = await updateCommodity(this.commodity)
      this.$message.success(baseRefault.message)
      this.dialogVisible = false
      this.findAll()
    }
    
  },
}
</script>

<style>

</style>